/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ejb;


import entity.User;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author user
 */

@Stateless
public class Userejb {
    
    @PersistenceContext(unitName = "meteocalpersist")
    EntityManager em;
    
    private User user;
    
    private List<User> users;

    public User checkRegistered(String username) {
        user = em.find(User.class, username);
        if (user != null)
            return user;
        else {
            return null;
        }
        
    }
       
    public void save(User user) {
        em.persist(user);
    }
    
    public List<User> returnallusers(){
        
        try {
            Query query = em.createQuery("SELECT u FROM User u");
            System.out.println("query fatta");
            users = query.getResultList();
            
        } catch (NoResultException e) {
            System.out.println("nel catch");
            
        }
        return users;
       
    }

}